package com.sgzn.sgpay.service.impl.bank;

import com.sgzn.sgpay.constants.BankVerifyInfoContants;
import com.sgzn.sgpay.model.request.bank.SgsBankFourElementsRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.service.bank.SgsBankVerifyService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

/**
 * Created by linliangfang 13580393408
 * DATETIME:2018/6/8 16:37
 * Description:
 */
@Service
@Slf4j
public class SgsBankVerifyServiceImpl implements SgsBankVerifyService {

    @Override
    public SgsResultResponse<String> verify(SgsBankFourElementsRequest sgsBankFourElementsRequest){

        SgsResultResponse sgsResultResponse = new SgsResultResponse();
        //请求参数
        String params = "bankcard="+ sgsBankFourElementsRequest.getBankcard();
        params += "&realName="+sgsBankFourElementsRequest.getRealName();
        params += "&cardNo="+sgsBankFourElementsRequest.getCertNo();  //可为空
        params += "&Mobile="+sgsBankFourElementsRequest.getMobile(); //可为空

        //发送请求
        log.info("银行卡验证请求参数:{}",params);
        String result = BankHttpUtils.APISTORE_GET(BankVerifyInfoContants.ALI_YUN_VERIFY_URL, params, BankVerifyInfoContants.ALI_YUN_VERIFY_APPCODE);
        //输出结果
        System.out.println(result);
        //JSON
        JSONObject object = JSONObject.fromObject(result);
        log.info("银行卡验证响应报文:{}",object.toString());
        sgsResultResponse.setErrorMsg(object.getString("reason"));
        sgsResultResponse.setErrorCode(object.getString("error_code"));
        return sgsResultResponse;
    }




    // 发起请求,获取内容
    public static void main(String[] args) {
        //请正确填写appcode,如果填写错误阿里云会返回 400错误或403错误
        //appcode查看地址 https://market.console.aliyun.com/imageconsole/
        String appcode = "15f66607714d4163b13547177fee9a60";
        //请求地址
        String url="http://aliyun-bankcard-verify.apistore.cn/bank";
        //请求参数
        String params = "bankcard=6227003320320012042";
        params += "&realName=林亮芳";
        params += "&cardNo=44142119840623555x";  //可为空
        params += "&Mobile=13580393408"; //可为空

        //发送请求
        String result = BankHttpUtils.APISTORE_GET(url, params, appcode);
        //输出结果
        System.out.println(result);
        //JSON
        JSONObject object = JSONObject.fromObject(result);
        //输出状态码
        System.out.println(object.getInt("error_code")) ;
        //输出返回结果
        System.out.println(object.get("reason")) ;
    }


}
